//
//  ZDWelcomeViewController.swift
//  1026SwiftSinaWeiBo
//
//  Created by Apple on 15/10/29.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SDWebImage

class ZDWelcomeViewController: UIViewController {
    
    //定义属性记录iconView的bottom约束  ———— 通过修改约束实现动画
    private var bottomConstraint: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()

       
        prepareUI()
        
        //可选绑定
        if let avatarURL = ZDAccount.loadAccountData()?.avatar_large {
        
            //下载头像
            iconView.zd_setImageWithURL(NSURL(string: avatarURL), placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    
    //MARK: - 控制器view显示后开始动画
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startIconViewRotation()
    }
    
    //MARK: - 头像动画
    private func startIconViewRotation() {
    
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.bottomConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            
            //重新布局
//            self.view.setNeedsLayout()   //通知重新布局  不会立即执行
            
            self.view.layoutIfNeeded()   //通知重新布局  立即重绘
            
            }) { (_) -> Void in
                
                //修改label的透明度
                UIView.animateWithDuration(0.3, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1.0
                    
                    }, completion: { (_) -> Void in
                        
                        //跳转到主控制器
                        (UIApplication.sharedApplication().delegate as! AppDelegate).switchController(ZDMainViewController())
                })
        }
    }
    
    //MARK: - 准备UI
    private func prepareUI() {
    
        //添加子控件
        view.addSubview(backgroundImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        
        //使用第三方 布局约束 背景View添加约束
        backgroundImageView.ff_Fill(view)
        
        //为头像添加约束
        iconView.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 85, height: 85), offset: CGPoint(x: 0, y: -160))
        
        //保存底部约束
        bottomConstraint = iconView.ff_Constraint(view.constraints, attribute: NSLayoutAttribute.Bottom)
        
        //为label添加约束
        welcomeLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 10))
        
//        //添加约束
//        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
//
//        //背景View添加约束  VFL语言
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : backgroundImageView]))
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bkg]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bkg" : backgroundImageView]))
//        
//        //为头像添加约束
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))   //centerX
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 85))    //width
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 85))  //height
//        
//        bottomConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -160)   //bottom
//        
//        view.addConstraint(bottomConstraint!)
//        
//        //为label添加约束
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
 
    }
    
    
    //MARK: - 背景view
    private lazy var backgroundImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    
    //MARK: - 头像
    private lazy var iconView: UIImageView = {
        
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
    
    
    //MARK: - label
    private lazy var welcomeLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "大圣归来"
        
        label.alpha = 0
        
        return label
    }()

}
